$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'logistics/logisticstype/list',
        datatype: "json",
        colModel: [			
			{ label: '物流ID', name: 'logisticsId', index: 'logistics_id', width: 50, key: true ,align:'center'},
			{ label: '物流名称', name: 'logisticsName', index: 'logistics_name', width: 80 ,align:'center'},
			{ label: '物流分类', name: 'logisticsCategory', index: 'logistics_category', width: 80, align:'center',
                formatter: function (value, options, row) {
                    var valueText = "";
                    var orderInfoStatusList = $("#jqGrid").jqGrid('getGridParam', 'userData').logisticsTypeEnum;
                    $.each(orderInfoStatusList, function (index, module) {
                        if (module.id == value) {
                            valueText = module.describe;
                            return false;
                        }
                    });
                    return valueText;
                }
			},
            { label: '排序', name: 'orderNum', index: 'order_num', width: 50 , align:'center'},
			{ label: '描述', name: 'describe', index: 'describe', width: 150 },
			{ label: '更新人', name: 'updateName', index: 'update_name', width: 80, align:'center'},
			{ label: '更新时间', name: 'updateTime', index: 'update_time', width: 120, align:'center'},
			{ label: '创建人', name: 'createName', index: 'create_name', width: 80, align:'center'},
			{ label: '创建时间', name: 'createTime', index: 'create_time', width: 120, align:'center'}
        ],
		viewrecords: true,
        height: window.innerHeight - 125,
        rowNum: 24,
		rowList : [24,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount",
            userdata: "userData"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        	vm.e.logisticsTypeEnum = $("#jqGrid").jqGrid('getGridParam', 'userData').logisticsTypeEnum;
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		logisticsType: {},
		e:{
            logisticsTypeEnum:[]
		},
        q:{
            logisticsName: '',
            logisticsCategory:''
        }
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.logisticsType = {};
		},
		update: function (event) {
			var logisticsId = getSelectedRow();
			if(logisticsId == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(logisticsId)
		},
		saveOrUpdate: function (event) {
            if(vm.validator()){
                return ;
            }
			var url = vm.logisticsType.logisticsId == null ? "logistics/logisticstype/save" : "logistics/logisticstype/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.logisticsType),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
            var logisticsId = getSelectedRow();
            if(logisticsId == null){
                return ;
            }
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "logistics/logisticstype/delete",
                    contentType: "application/json",
				    data: JSON.stringify(logisticsId),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(logisticsId){
			$.get(baseURL + "logistics/logisticstype/info/"+logisticsId, function(r){
                vm.logisticsType = r.logisticsType;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page,
                postData:{
                    'logisticsName': vm.q.logisticsName,
					'logisticsCategory':vm.q.logisticsCategory
                },
            }).trigger("reloadGrid");
		},
        validator: function () {
            console.log(vm.logisticsType);
            if(isBlank(vm.logisticsType.logisticsName)){
                alert("物流名称不能为空");
                return true;
            }
            //菜单
            if( !(vm.logisticsType.logisticsCategory >= 0)){
                alert("物流分类不能为空");
                return true;
            }
        }
	}
});